﻿using DataProcess.Interface;
using DataProcess.Interface.Models;
using System;
using System.Linq;
using System.Text.RegularExpressions;

namespace DataProcessing.PANAlyticalXRF
{
    public class DataProcessing : IDataProcessing
    {
        public string Name { get { return "PANAlytical(帕纳科) 荧光仪"; } }

        public IDataOutput Processing(string content)
        {
            XRayOutputModel outputModel = new XRayOutputModel();
            var regContext = new Regex("[\r\n]+").Replace(content, "\r");
            string[] spdata = regContext.Split('\r');
            for (int i = 0; i < spdata.Length - 1; i++)
            {
                var array = new Regex("[\\s]+").Split(spdata[i]);
                if (array.Length <= 1) continue;
                int lastIndex = 1;
                switch (array[0])
                {
                    case "S":
                        if (array[array.Length - 1] == "")
                        {
                            lastIndex = 2;
                        }

                        if (array.Length - 1 - lastIndex >= 1)
                        {
                            for (int j = 1; j <= array.Length - 1 - lastIndex; j++)
                            {
                                outputModel.SampleName = $"{outputModel.SampleName}{array[j]} ";
                            }
                            outputModel.SampleName = outputModel.SampleName.Trim();
                        }
                        else
                        {
                            outputModel.SampleName = array[1];
                        }

                        if (DateTime.TryParseExact(array[array.Length - lastIndex], "yyMMddhhmm", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out DateTime date))
                        {
                            outputModel.MeasureTime = date;
                        }
                        else
                        {
                            outputModel.MeasureTime = DateTime.Now;
                        }
                        break;
                    case "A":
                        if (array[array.Length - 1] == "")
                        {
                            lastIndex = 2;
                        }

                        for (int j = 1; j <= array.Length - lastIndex; j++)
                        {
                            outputModel.AnalyzeApplicationName = $"{ outputModel.AnalyzeApplicationName} {array[j]} ";
                        }
                        if (outputModel.AnalyzeApplicationName != null)
                            outputModel.AnalyzeApplicationName = outputModel.AnalyzeApplicationName.Trim();
                        break;
                    case "D":
                        if (spdata.Any(it => it != "" && it[0] == 'A')) continue;
                        if (array[array.Length - 1] == "")
                        {
                            lastIndex = 2;
                        }

                        if (array.Length - 2 - lastIndex >= 2)
                        {
                            for (int j = 2; j < array.Length - 2 - lastIndex; j++)
                            {
                                outputModel.AnalyzeApplicationName = $"{outputModel.AnalyzeApplicationName}{array[j]} ";
                            }
                            outputModel.AnalyzeApplicationName = outputModel.AnalyzeApplicationName.Trim();
                        }
                        else
                        {
                            outputModel.AnalyzeApplicationName = array[2];
                        }
                        break;
                    case "C":
                        if (array.Length < 5) continue;

                        if (!outputModel.Eelements.ContainsKey(array[4]))
                        {
                            if (double.TryParse(array[2], out _)) outputModel.Eelements.Add(array[4], array[2]);
                        }
                        break;
                }
            }
            return outputModel;
        }
    }
}
